package com.dhl.cloud.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dhl.cloud.common.Constants;
import com.dhl.cloud.common.http.HttpResult;
import com.dhl.cloud.common.util.PasswordUtils;
import com.dhl.cloud.common.vdo.UserBean;
import com.dhl.cloud.common.vo.LoginFrontBean;
import com.dhl.cloud.core.entity.Role;
import com.dhl.cloud.core.entity.User;
import com.dhl.cloud.core.entity.UserAndRole;
import com.dhl.cloud.core.service.RoleService;
import com.dhl.cloud.core.service.UserService;
import com.dhl.cloud.security.JwtUtil;

@RestController
@RequestMapping(value = "/open")
public class LoginController {

	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	
	@Value("${token.tokenExpireTime}")
	private String tokenExpireTime;

	@PostMapping(value = "/login")
	public HttpResult login(@RequestBody LoginFrontBean loginBean, HttpServletRequest request) {

		String username = loginBean.getUsername();
		String password = loginBean.getPassword();
		// 用户信息
		User user = userService.selectByUsername(username);
		// 账号不存在、密码错误
		if (user == null) {
			return HttpResult.error(Constants.ERROR_MSG_3);
		}
		if (!PasswordUtils.matches(Constants.SALT, password, user.getPassword())) {
			return HttpResult.error(Constants.ERROR_MSG_4);
		}

		// 系统登录认证---后面在加上
		UserBean ub = new UserBean();
		ub.setPersonname(user.getPersonname());
		userService.updateUserOnlineFlag(username,1);
		List<UserAndRole> roleList = userService.selectByUsernameAndTenantid(username,"1000000000000");
		ub.setRoleList(roleList);
		Long currentTimeMillis = System.currentTimeMillis();
		String strCurrentTimeMillis = String.valueOf(currentTimeMillis);
		String token = JwtUtil.sign(username, "1000000000000",strCurrentTimeMillis,tokenExpireTime);
		ub.setToken(token);
		ub.setUserimgurl(user.getUserimgurl());
		ub.setUsername(username);
		ub.setUserid(user.getId());
		//token存redis；
//		redis.set(Constants.USER_TOKEN+username,token);


		return HttpResult.success("",ub);
	}
	
	@PostMapping(value = "/regeister")
	public HttpResult regeister(@RequestBody LoginFrontBean loginBean, HttpServletRequest request) {

		String username = loginBean.getUsername();
		// 用户信息
		User user = userService.selectByUsername(username);
		// 账号不存在、密码错误
		if (user != null) {
			return HttpResult.error(Constants.ERROR_MSG_10);
		}
		int flag = userService.regeister(loginBean);
	
		return flag==1 ? HttpResult.success("注册成功，请到登录页面重新登录"):HttpResult.error(Constants.ERROR_MSG_25);
	}
	
	/**
	 * 用户注册的时候校验用户名
	 * @param username：登录名
	 * @return 返回是否存在
	 */
	@GetMapping("checkUsername")
	public HttpResult checkUsername(String username)
	{
		User user = userService.selectByUsername(username);
		if (user != null && user.getUserstatus()!=0)
		{
			return HttpResult.error(Constants.ERROR_MSG_10);
		}
		return HttpResult.success("");
	}
	
	/**
	 * 获取所有角色列表
	 */
	@GetMapping("getRoleList")
	public HttpResult getRoleList()
	{
		List<Role> list = roleService.getRoleList();
		return HttpResult.success("",list);
	}

}
